//
//  UIShareLittleViewController.m
//  HouseShow
//
//  Created by gongjie on 12-1-2.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIShareLittleViewController.h"
#import "ASIFormDataRequest.h"
static WeiBo* weibo = nil;
@implementation UIShareLittleViewController
@synthesize m_SendImg;
//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//    }
//    return self;
//}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{

    [self.view setBackgroundColor:[UIColor clearColor]];
    UIView* zhongjian = [[UIView alloc] initWithFrame:CGRectMake(240, 0, 540, 1024)];
    [zhongjian setBackgroundColor:[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0f]];
    
    UIView* hend = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 540, 44)];
    [hend setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"hand.png"]]];
    
    UIButton* Lbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    Lbutton.frame = CGRectMake(10, 4, 60, 34);
    [Lbutton setTitle:@"返回" forState:UIControlStateNormal];
    [Lbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
	[Lbutton addTarget:self action:@selector(changerightBarButtonItem) forControlEvents:UIControlEventTouchUpInside];
    [hend addSubview:Lbutton];
	
    UIButton* ibutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ibutton.frame = CGRectMake(470, 4, 60, 34);
    [ibutton setTitle:@"分享" forState:UIControlStateNormal];
    [ibutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [ibutton addTarget:self action:@selector(changeleftBarButtonItem) forControlEvents:UIControlEventTouchUpInside];
    [hend addSubview:ibutton];
    [zhongjian addSubview:hend];
    [hend release];
    microbocontent = [[UITextView alloc] initWithFrame:CGRectMake(20, 100, 500, 140)];
    [microbocontent becomeFirstResponder];
    [microbocontent setReturnKeyType:UIReturnKeyDone];
    [microbocontent setFont:[UIFont systemFontOfSize:20]];
    microbocontent.keyboardType = UIKeyboardTypeNamePhonePad;
    [zhongjian addSubview:microbocontent];
    
 
    
    tip = [[UILabel alloc] initWithFrame:CGRectMake(30, 70, 250, 22)];
    [tip setBackgroundColor:[UIColor clearColor]];
    tip.font = [UIFont systemFontOfSize:20.0f];
    tip.text = @"请输入您的姓名和手机号";
    tip.textColor = [UIColor blackColor];
    [zhongjian addSubview:tip];
    description = [[UILabel alloc] initWithFrame:CGRectMake(69, 245, 350, 17)];
    [description setBackgroundColor:[UIColor clearColor]];
    description.font = [UIFont systemFontOfSize:15.0];
    description.text = @"分享到新浪微博可以让你的朋友也体验到万科的精彩内容，如果您还没有新浪微博帐号，请到新浪网站注册。";
    description.textColor = [UIColor colorWithRed:93/255 green:102/255 blue:121/255 alpha:1.0f];
    description.numberOfLines = 0;
    description.lineBreakMode = UILineBreakModeWordWrap;
    [zhongjian addSubview:description];
    [self.view addSubview:zhongjian];
}

-(void) changerightBarButtonItem
{
     
    [microbocontent resignFirstResponder];
    self.view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.2];
    self.view.frame = CGRectMake(0, ScreenHeight, ScreenWidth, ScreenHeight);
    [UIView commitAnimations];


}
-(void) changeleftBarButtonItem
{
        if (microbocontent.text!=nil&&microbocontent.text!=@"") {
            if (weibo == nil) {
                weibo = [[WeiBo alloc] initWithAppKey:@"1292912084" withAppSecret:@"9651b053d439aeed1cc923c3e4833cfc"];
                weibo.delegate = self;
            }
            [weibo startAuthorize];
        }else{
            microbocontent.text =@"";
            UIAlertView* tAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入正确的文字" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil];
            tAlertView.tag =1004;
            [tAlertView show];
            [tAlertView release];
        }
    
}

+(WeiBo*) GetWeiBo{
    return weibo;
}

- (void)weiboDidLogin
{
//	buttonLogout.hidden = FALSE;
//	buttonSend.hidden = FALSE;
//	buttonGetFeed.hidden = FALSE;
	
	UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil 
													   message:@"用户验证已成功！" 
													  delegate:nil 
											 cancelButtonTitle:@"确定" 
											 otherButtonTitles:nil];
    alertView.tag = 10000;
    alertView.delegate =self;
	[alertView show];
	[alertView release];
    
}

- (void)weiboLoginFailed:(BOOL)userCancelled withError:(NSError*)error
{
	UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"用户验证失败！"  
													   message:userCancelled?@"用户取消操作":[error description]  
													  delegate:nil
											 cancelButtonTitle:@"确定" 
											 otherButtonTitles:nil];
	[alertView show];
	[alertView release];
}

- (void)weiboDidLogout
{
//	buttonLogout.hidden = TRUE;
//	buttonSend.hidden = TRUE;
//	buttonGetFeed.hidden = TRUE;
	
	UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil 
													   message:@"用户已成功退出！" 
													  delegate:nil 
											 cancelButtonTitle:@"确定" 
											 otherButtonTitles:nil];
	[alertView show];
	[alertView release];
}


- (void)request:(WBRequest *)request didLoadRawResponse:(NSData *)data{
    NSLog(@"%@",[data description]);
    
	UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:nil 
													   message:@"微博分享成功。" 
													  delegate:nil 
											 cancelButtonTitle:@"确定" 
											 otherButtonTitles:nil];
    alertView.tag = 10001;
    alertView.delegate = self;
	[alertView show];
	[alertView release];
}

-(void)SendMicrobo:(NSString*)content
{
    
    [weibo postWeiboRequestWithText:content andImage:m_SendImg andDelegate:self];

}





- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag ==10000) {
        [self SendMicrobo:microbocontent.text];
    }else if(alertView.tag == 10001)
    {
        [self changerightBarButtonItem];
    }
    
}
-(void)dealloc
{
    [m_SendImg release];
    [microbocontent release];
    [tip release];
    [description release];
    [weibo release];
    [super dealloc];
}

@end
